// Simulation of Price Revelation MOdel
clear all
/*------------ Parameters ---------------------------------------------------*/
/* Time periods */
scalar periods=200
scalar p_0=periods/2
scalar p_1=p_0+1
/* Individuals */
scalar individual=10000
global observations=periods*individual
set seed 111
/* Simulation Iterations */
global iter=10000
/*-------------------------------------------------------------------------*/
	
// Running Programs
program main
	simulate_alternate_thetas
	simulate_individual_paths
end	
	
// Program to Generate Data
program data_gen
	clear
	set obs $observations
	/* Derived Parameters */
	scalar lambda = 1/(sqrt(sigma2_m)) * (normalden(0)/normal(0))
	scalar delta_1=beta*(lambda+gamma)
	
	/* Defining Observation ids */
	gen id=_n
	replace id=ceil(id/periods)
	
	/* Defining variances of messages */
	bys id: gen n_aux=_n 
	gen sigma=sqrt(beta*theta^(-(n_aux-p_1)))	
	bys id (n): replace sigma=0 if n<p_1
	bys id (n_aux): gen remain_var_aux=sum(sigma^2)
	bys id (n_aux): gen remain_var=remain_var_aux[_n-1]
	replace remain_var=0 if n_aux==1
	
	
	replace remain_var=0 if remain_var==.
	
	/* Drawing Messages */
	gen m_t=rnormal(0,sigma)
	gen n=periods+1-n_aux
	sort id n
	
	/* Summing Realized m */
	bys id (n): gen m_exp=sum(m_t)
	bys id (n): replace m_exp=m_exp[_n-1] if n>p_0
	
	/* Defining Price */
	gen price=m_exp-gamma*remain_var
	
	/* Defining Success */
	bys id: gen success_aux=(m_exp>0) if n==periods
	bys id: egen success=max(success_aux)
	drop success_aux
	
	/* Defining Treatment */
	gen treated=success==1& n>=p_0
	gen treat_all=n>=p_0
	
	/* Results */
	xtset id n
end

program simulate_alternate_thetas
	set seed 111
	/* Setting Parameters For Simulation with low theta */
	scalar gamma = 0.2  // Risk aversion
	scalar theta = 1.05  // Exponential Variance Parameter
	scalar sigma2_m=3
	scalar beta  = sigma2_m*(1-1/theta)    // Variance of Final revelationdata_gen
	data_gen
	replace n=n-p_0
	collapse (mean) price, by(success n)
	rename price price_1
	replace price_1=price_1+0.03
	tempfile temp1
	save `temp1', replace
	
	clear

/* Setting Parameters for simulation with high theta */
	scalar gamma = 0.2  // Risk aversion
	scalar theta = 2  // Exponential Variance Parameter
	scalar sigma2_m=2.9
	scalar beta  = sigma2_m*(1-1/theta)    // Variance of Final revelationdata_gendata_gen
	data_gen

	replace n=n-p_0
	collapse (mean) price, by(success n)
	rename price price_2
	tempfile temp2
	save `temp2', replace


	use `temp1', clear
	merge 1:1 success n using `temp2'
	
	gen price_3=-0.58 if n<0
	replace price_3=1.3775 if n>=0
	
	twoway (line price_1 n if success==1, lcolor(red%50) lwidth(thick))  ///
		   (line price_2 n if success==1, lcolor(blue%50) lwidth(thick)) ///
		   (line price_3 n if success==1, lcolor(black%80) lwidth(thick) connect(stairstep)), ///
		graphregion(color(white)) xline(0, lcolor(black)) ylabel("",nogrid) ///
		ytitle("Expected Price") xtitle("Period") xlabel(0) legend(off) 
		graph export ../gph/simulate_theta.pdf, replace font(palatino)
end

program simulate_individual_paths
	set seed 111
	/* Setting Parameters */
	scalar gamma = 0.2  // Risk aversion
	scalar theta = 1.05  // Exponential Variance Parameter
	scalar sigma2_m=3
	scalar beta  = sigma2_m*(1-1/theta)    // Variance of Final revelationdata_gen
	data_gen
	bys n success: egen mean_p=mean(price)

	replace n=n-p_0
	forvalues x=1(1)100{
		local z "`z' (line price n if id==`x' & success==1, color(blue%10) lpattern(solid))"
	}
	

	twoway  ///
	`z'  (line mean_p n if success==1 & id==2, lpattern(solid)lwidth(thick) color(blue%50)) , ///
	xtitle(Period) ytitle(Price) ylabel("", nogrid) xlabel(0) graphregion(color(white)) legend(off)  xline(0, lcolor(black))
	graph export ../gph/simulate_success.pdf, replace font(palatino)
end


main
